package com.hao.study.controller;

import com.github.pagehelper.PageInfo;
import com.hao.study.common.APIResponse;
import com.hao.study.common.ResponeCode;
import com.hao.study.entity.Resume;
import com.hao.study.permission.Access;
import com.hao.study.permission.AccessLevel;
import com.hao.study.service.ResumeService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @author: author
 * @description:    简历
 * @date: 2024/6/12 13:23
 * @version: 1.0
 */
@Api(tags={"简历管理接口"})
@RestController
@RequestMapping("/resume")
public class ResumeController {
    @Autowired
    ResumeService service;

    @Value("${File.uploadPath}")
    private String uploadPath;

    @GetMapping("/list")
    public APIResponse list(@RequestParam(defaultValue = "1") Integer pageNum,
                            @RequestParam(defaultValue = "10") Integer pageSize,
                            String keyWord){
        PageInfo<Resume> list = service.getList(pageNum,pageSize,keyWord);
        return new APIResponse<>(ResponeCode.SUCCESS,"查询成功",list);
    }

    @Access(level = AccessLevel.ADMIN)
    @PostMapping("/create")
    @Transactional
    public APIResponse crete(Resume resume) throws IOException {
//        saveResume(resume);
        service.createResume(resume);
        return new APIResponse<>(ResponeCode.SUCCESS,"创建成功");
    }

    @Access(level = AccessLevel.ADMIN)
    @PostMapping("/delete")
    public APIResponse delete(String ids){
        System.out.println("ids=====>"+ids);
        String[] arr = ids.split(",");
        for (String id:arr){
            service.deleteById(id);
        }
        return new APIResponse<>(ResponeCode.SUCCESS,"删除成功");
    }

    @Access(level = AccessLevel.ADMIN)
    @PostMapping("/update")
    @Transactional
    public APIResponse update(Resume resume) throws IOException {
//        saveResume(resume);
        service.updateResume(resume);
        return new APIResponse<>(ResponeCode.SUCCESS,"更新成功");
    }

    @GetMapping("/detail")
    public APIResponse detail(String userId){
        Resume resume =  service.getDetail(userId);

        return new APIResponse(ResponeCode.SUCCESS, "查询成功", resume);
    }

//    public void saveResume(Resume resume) throws IOException {
//        MultipartFile file = resume.getCoverFile();
//        String newFileName = null;
//        if(file !=null && !file.isEmpty()) {
//
//            // 存文件
//            String oldFileName = file.getOriginalFilename();
//            String randomStr = UUID.randomUUID().toString();
//            newFileName = randomStr + oldFileName.substring(oldFileName.lastIndexOf("."));
//            String filePath = uploadPath + File.separator + "resume" + File.separator + newFileName;
//            File destFile = new File(filePath);
//            if(!destFile.getParentFile().exists()){
//                destFile.getParentFile().mkdirs();
//            }
//            file.transferTo(destFile);
//        }
//        if(!StringUtils.isEmpty(newFileName)) {
//            resume.setCover(newFileName);
//        }
//    }

}
